package com.itboye.sunsun.news;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.NewsHotReadAdapter;
import com.itboye.sunsun.beans.NewsHotReaBean;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.web.ui.WebActivity;

public class NewsFragment extends BaseFragment implements IXListViewListener {

	ArrayList<NewsHotReaBean.AllData.AllList> arrayList;
	ArrayList<NewsHotReaBean.AllData.AllList>arrayListCopy;
	XListView news_lst;
	int count = 6;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_newshotread_item, null);
	}

	private int index;
	private View view;
	protected int page = 1;
	private NewsHotReadAdapter adapter;

	public NewsFragment(int index) {
		this.index = index;

	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		showProgressDialog("数据正在加载中，请稍后", true);
		arrayListCopy=new ArrayList<NewsHotReaBean.AllData.AllList>();
		arrayList = new ArrayList<>();
		adapter = new NewsHotReadAdapter(arrayList, getActivity());
		news_lst.setAdapter(adapter);
		news_lst.setPullLoadEnable(true);
		news_lst.setPullRefreshEnable(true);
		news_lst.setXListViewListener(this);
		news_lst.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (arrayList != null) {
					if (arrayList.size() > 0) {
						Intent intent = new Intent(getContext(),
								WebActivity.class);
						intent.putExtra("url",
								NetPublicConstant.secondUrl+"/Webview/Post/viewPost?id="
										+ arrayListCopy.get(position-1).getId()+"&from=app");
						intent.putExtra("content", arrayListCopy.get(position-1)
								.getPostExcerpt());
						intent.putExtra("title", "新闻详情");
						intent.putExtra("title2",arrayListCopy.get(position-1)
								.getPostTitle());
						intent.putExtra("img", arrayListCopy.get(position-1).getMainImg());
						startActivity(intent);
					}
				}
			}
		});
		view = LayoutInflater.from(getActivity()).inflate(
				R.layout.news_list_header, null);
		view.findViewById(R.id.img).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (arrayList != null) {
					if (arrayList.size() > 0) {
						Intent intent = new Intent(getContext(),
								WebActivity.class);
						intent.putExtra("url",
								NetPublicConstant.secondUrl+"/Webview/Post/viewPost?id="
										+ arrayListCopy.get(0).getId()+"&from=app");
						intent.putExtra("content", arrayListCopy.get(0)
								.getPostExcerpt());
						intent.putExtra("title", "新闻详情");
						intent.putExtra("title2", arrayListCopy.get(0).getPostTitle());
						intent.putExtra("img", arrayListCopy.get(0).getMainImg());
						startActivity(intent);
					}
				}
			}
		});
		getData(1);
	}

	// 得到资讯热读里面的数据
	public void getData(final int type) {
		if (type == 0) {// 上拉加载更多

		} else {// 下拉刷新
			page = 1;
		}
		MyJsonRequest<NewsHotReaBean.AllData> request = new MyJsonRequest.Builder<NewsHotReaBean.AllData>()
				.apiVer("100")
				.typeKey("BY_News_query")
				.param("position", index)
				.param("page_no", page)
				.requestListener(
						new XRequestListener<NewsHotReaBean.AllData>() {

							@Override
							public void onResponse(NewsHotReaBean.AllData arg0) {
								if (arg0 != null) {
									if (arg0.getList() != null) {

										news_lst.removeHeaderView(view);// 先移除再添加
										news_lst.addHeaderView(view);

										if (type == 1)
											arrayList.clear();

										page++;

										arrayList.addAll(arg0.getList());
										arrayListCopy.addAll(arg0.getList());
//										arrayList_id.add(arrayList.get(0).getId());
//										array_bt.add(arrayList.get(0).getMainImg());
										
////										
//										for (int i = 0; i < arg0.getList()
//												.size(); i++) {
//											arrayList_id.add(arg0.getList()
////													.get(i).getId());
//											array_bt.add(arg0.getList().get(i)
//													.getMainImg());
//										}

										XImageLoader.load(arrayList.get(0).getMainImg(),
												(ImageView) view
														.findViewById(R.id.img));
										arrayList.remove(0);
									}

								}

								if (arg0.getCount() <= page * count) {
									news_lst.iniFooder(getActivity());

								} else {
									news_lst.iniFooerMore(getActivity());
								}

								adapter.notifyDataSetChanged();
								stopRefresh();
								closeProgressDialog();
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						stopRefresh();
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	public void stopRefresh() {
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}

		news_lst.stopRefresh();
		news_lst.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		getData(1);

	}

	@Override
	public void onLoadMore() {
		getData(0);
	}

}
